package com.kid.keep.bo;

import cn.hutool.core.date.DateUtil;
import com.kid.keep.enums.BizExceptionEnum;
import com.kid.keep.enums.DateTypeEnum;
import com.kid.keep.exception.BizException;
import lombok.Data;
import org.apache.commons.lang.time.DateUtils;

import java.util.Date;
import java.util.Objects;

/**
 * @Description 统计时间
 * @Author JOHN
 * @Create 2020-01-13
 * @Since 1.0.0
 */
@Data
public class StatisticsDate {
    Date startDate;
    Date endDate;

    public static StatisticsDate getDate(Integer type) {
        DateTypeEnum dateTypeEnum = DateTypeEnum.getEnum(type);
        if (Objects.isNull(dateTypeEnum)) {
            throw BizException.getInstance(BizExceptionEnum.STATISTICS_DATE_TYPE_INVALID);
        }
        StatisticsDate statisticsDate;
        Date date = new Date();
        switch (dateTypeEnum) {
            case WEEK: {
                statisticsDate = new StatisticsDate();
                statisticsDate.setStartDate(DateUtil.beginOfDay(DateUtils.addDays(date,-7)));
                statisticsDate.setEndDate(DateUtil.endOfDay(date));
                break;
            }
            case MONTH: {
                statisticsDate = new StatisticsDate();
                statisticsDate.setStartDate(DateUtil.beginOfMonth(date));
                statisticsDate.setEndDate(DateUtil.endOfMonth(date));
                break;
            }
            case YEAR: {
                statisticsDate = new StatisticsDate();
                statisticsDate.setStartDate(DateUtil.beginOfYear(date));
                statisticsDate.setEndDate(DateUtil.endOfYear(date));
                break;
            }
            default: {
                throw BizException.getInstance(BizExceptionEnum.STATISTICS_DATE_TYPE_INVALID);
            }
        }
        return statisticsDate;
    }
}
